<template>
	<form v-if="form" report-submit @submit="jumpLogic">
		<!-- #ifndef MP-ALIPAY -->
		<!-- :open-type="open_type === 'contact' ? (wx.openCustomerServiceChat ? '' : 'contact') : (open_type === 'share' ? 'share' : '')" -->
		<button :open-type="open_type === 'contact' ? '' : (open_type === 'share' ? 'share' : '')" hover-class="none"
			:style="{'background-color': `${backgroundColor ? backgroundColor : '' }`, 'height': `${height}%`, 'width': `${width}%`}"
			:class="[`${arrangement === 'column' ? 'app-button-column' : arrangement === 'row' ? 'app-button-row' : arrangement === 'left' ? 'app-button-left': arrangement === 'topCenter' ? 'app-button-top-cross-center' : arrangement === 'a' ? 'app-left' : arrangement === 'b' ? 'app-left-between' : ''}`]"
			formType="submit">
			<slot></slot>
		</button>
		<!-- #endif -->
		<!-- #ifdef MP-ALIPAY -->
		<template>
			<button :open-type="open_type === 'share' ? 'share' : ''" hover-class="none"
				:style="{'background-color': `${backgroundColor ? backgroundColor : '' }`, 'height': `${height}%`, 'width': `${width}%`}"
				:class="[`${arrangement === 'column' ? 'app-button-column' : arrangement === 'row' ? 'app-button-row' : arrangement === 'left' ? 'app-button-left': arrangement === 'topCenter' ? 'app-button-top-cross-center' : arrangement === 'a' ? 'app-left' : arrangement === 'b' ? 'app-left-between' : ''}`]"
				formType="submit">
				<slot></slot>
			</button>
		</template>
		<!-- #endif -->
	</form>
	<button v-else :open-type="open_type === 'contact' ? 'contact': ''" hover-class="none"
		:style="{'background-color': `${backgroundColor ? backgroundColor : '' }`, 'height': `${height}%`, 'width': `${width}%`}"
		:class="[`${arrangement === 'column' ? 'app-button-column' : arrangement === 'row' ? 'app-button-row' : arrangement === 'left' ? 'app-button-left': arrangement === 'topCenter' ? 'app-button-top-cross-center' : arrangement === 'a' ? 'app-left' : arrangement === 'b' ? 'app-left-between' : ''}`]"
		@click="jumpLogic">
		<slot></slot>
	</button>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		push
	} from '../../../core/formId.js';
	import {
		clearStorage
	} from '../../../core/cache.js';
	// import siteInfo from '../../../siteinfo.js'
	// let pageroute = ''
	export default {
		name: 'app-jump-button',
		data() {
			return {
				status: 0
			}
		},
		props: {
			item: Object,
			itename: {
				type: String
			},
			arrangement: {
				type: String,
				default: function() {
					return 'row';
				}
			},
			backgroundColor: String,
			form: {
				type: Boolean,
				default: function() {
					return true;
				}
			},
			height: {
				type: String,
				default: function() {
					return '100';
				}
			},
			width: {
				type: String,
				default: function() {
					return '100';
				}
			},
			open_type: {
				type: String,
				default: function() {
					return 'navigate';
				}
			},
			url: {
				type: String,
				default: function() {
					return '';
				}
			},
			params: {
				type: Array,
				default: function() {
					return [];
				}
			},
			number: {
				type: String,
				default: function() {
					return '';
				}
			},
			appId: {
				type: String,
				default: function() {
					return '';
				}
			},
			path: {
				type: String,
				default: function() {
					return '';
				}
			},
			latitude: {
				type: String,
				default: function() {
					return '0';
				}
			},
			longitude: {
				type: String,
				default: function() {
					return '0';
				}
			},
			address: {
				type: String,
				default: function() {
					return '';
				}
			},
			marginA: {
				type: String,
				default: function() {
					return '';
				}
			},
		},
		onShareAppMessage(res) {
			console.log("我分享了 neriiiiiiiiiiiii");
			if (res.from === 'button') {
				console.log(res.target, 'fenxiang');
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.user.info
			})
		},
		// mounted() {
		// 	let pages = getCurrentPages()
		// 	pageroute = pages[pages.length - 1].route
		// 	console.log(pageroute);
		// },
		methods: {
			appStatus(id) {
				let self = this
				let status = 0
				self.$request({
					url: self.$api.apply.store,
					data: {
						id: id
					},
					method: 'get',
				}).then(res => {
					if (res.data.review_status == 0) {
						self.status = 0
						uni.navigateTo({
							url: '/pages/bepartner/index'
						})
						return;
					} else if (res.data.review_status == 1) {
						self.status = 1
						uni.redirectTo({
							url: '/pages/store/mine/mine'
						});
						return;
					} else if (res.data.review_status == 2) {
						self.status = 2
						uni.navigateTo({
							url: '/pages/bepartner/index'
						})
						return;
					}
				}).catch(e => {

				})
			},
			jumpLogic(e) {

				if (this.form) push(e.detail.formId);
				switch (this.open_type) {
					case 'reLaunch':
						if (this.url == 'javascript:;' || this.url == '/' || this.url == '') {
							return;
						}
						uni.reLaunch({
							url: this.url,
						});
						break;
					case 'redirect':
						if (this.url == 'javascript:;' || this.url == '/' || this.url == '') {
							return;
						}
						uni.redirectTo({
							url: this.url
						});
						break;
					case 'navigate':

						// 12.20 注释
						/* if (this.url == 'javascript:;' || this.url == '/' || this.url == '' || this.url.indexOf(
								'/plugins/mch/goodslist/goodslist') != -1 || this.url.indexOf('/plugins/pt/index/index') !=
							-1 || this.url.indexOf('/pages/goods/list') != -1) {
							this.$emit('btclick', this.url)
							return;
						} */
						if (this.url == 'javascript:;' || this.url == '/' || this.url == undefined || this.url == '') {
							return;
						}
						if (this.url) {
							let url = this.url;
							if (this.params.length > 0) {
								let p = `?`;
								for (let i = 0; i < this.params.length; i++) {
									p += `${this.params[i].key}=${this.params[i].value}&`
								}
								url = url.split('?')[0];
								url += p.slice(0, p.length - 1);

							}
							// 12.20
							if (this.url.indexOf('/plugins/mch/goodslist/goodslist') != -1) {
								this.$emit('btclick', this.url)

								return;
							}
							if (this.url.indexOf('/plugins/pt/index/index') != -1 || this.url.indexOf(
									'/pages/goods/list') != -1) {
								if (this.item && this.item.name) {
									if (url.indexOf('?') == -1) {
										url = url + '?title=' + this.item.name
									} else {
										url = url + '&title=' + this.item.name
									}
								}

								uni.navigateTo({
									url: url
								})

								return;
							}
							if (this.url == '/pages/index/index' || this.url.indexOf('/pages/gospeed/index') != -1 || this
								.url == '/pages/sixty/index' || this.url == '/pages/user-center/user-center' || this.url ==
								'/pages/store/mine/mine') {
								// uni.navigateBack({
								// 	delta: 5
								// })
								uni.reLaunch({
									url: this.url
								})

								return;
							}
							if (this.url == '/pages/user-center/contactus/contactus') {
								uni.navigateTo({
									url: '/pages/user-center/contactus/contactus?title=' + this.item.name
								})
								return;
							}

							if (this.url == '/pages/order/index/index') {
								uni.navigateTo({
									url: '/pages/order/index/index?isrefund=1'
								})
								return;
							}
							if (this.url == '/pages/coupon/index/index' || this.url == '/pages/pt/apelled/apelled' || this
								.url == '/plugins/check_in/index/index' || this.url ==
								'pages/user-center/personal/personal' || this.url == '/plugins/pond/index/index' || this
								.url == '/plugins/integral_mall/index/index' || this.url.indexOf(
									'/plugins/pt/detail/detail') != -1 || this.url ==
								'/pages/user-center/personal/personal' || this.url == '/pages/pt/waitspe/waitspe' || this
								.url == '/pages/bepartner/index') {
								if (this.$store.state.user.accessToken === '' || this.$store.state.user.accessToken ===
									null) {
									this.$store.dispatch('user/accessToken');
									return;
								} else {
									uni.navigateTo({
										url: url
									});
									return;
								}
							}
							if (this.url == '/pages/bepartner/index') {
								if (this.userInfo.vdian_id != null) {
									this.appStatus(this.userInfo.vdian_id)
									return;
								} else {
									uni.navigateTo({
										url: url
									});
								}
							} else {
								uni.navigateTo({
									url: url
								});
							}
						}
						break;
					case 'app_admin':
						if (this.$store.state.user.info.identity.is_admin == 1) {
							uni.navigateTo({
								url: this.url,
							});
						}
						break;
					case 'back':
						uni.navigateBack({});
						break;
					case 'tel':
						if (this.params.length === 1) {
							uni.makePhoneCall({
								phoneNumber: this.params[0].value
							});
						} else if (this.number) {
							uni.makePhoneCall({
								phoneNumber: this.number
							});
						} else {
							uni.makePhoneCall({
								phoneNumber: this.url.split('?')[1].split('=')[1],
							});
						}
						break;
					case 'web':
						if (this.params.length > 0) {
							uni.navigateTo({
								url: `${this.url}?url=${encodeURIComponent(this.params[0].value)}`
							});
						} else {
							uni.navigateTo({
								url: this.url
							});
						}
						break;
					case 'app':
						if (this.url) {
							let originalPath = '';
							if (this.url !== 'app') {
								originalPath = this.url.split('app?')[1];
							} else {
								for (let i = 0; i < this.params.length; i++) {
									originalPath += `${this.params[i].key}=${this.params[i].value}&`;
								}
							}
							if (typeof originalPath !== 'string') return;
							let groups = originalPath.split('&');
							let obj = {};
							for (let i in groups) {
								if (typeof groups[i] !== 'string') continue;
								if (!groups[i].length) continue;
								let kvs = groups[i].split('=');
								if (kvs.length !== 2) {
									let s = '';
									for (let i = 1; i < kvs.length; i++) {
										if (i + 1 === kvs.length) {
											continue;
										} else {
											s += `${kvs[i]}=${kvs[i+1]}`
										}
									}
									obj[kvs[0]] = s;
								} else {
									obj[kvs[0]] = kvs[1];
								}
							}
							let params = obj;
							let appId = '';
							let path = '';
							// #ifdef MP-WEIXIN
							appId = params.app_id || '';
							path = params.path || '';
							// #endif
							// #ifdef MP-ALIPAY
							appId = params.ali_app_id || '';
							path = params.ali_path || '';
							// #endif
							// #ifdef MP-TOUTIAO
							appId = params.tt_app_id || '';
							path = params.tt_path || '';
							// #endif
							// #ifdef MP-BAIDU
							appId = params.bd_app_key || '';
							path = params.bd_path || '';
							// #endif
							uni.navigateToMiniProgram({
								appId: appId,
								path: path
							});
						} else if (this.appId) {
							uni.navigateToMiniProgram({
								appId: this.appId,
								path: this.path ? this.path : ''
							});
						}
						break;
					case 'clear_cache':
						uni.showModal({
							content: '确定要清理缓存？',
							cancelText: '取消',
							confirmText: '确认',
							success: (e) => {
								if (e.confirm) {
									// uni.showLoading({
									//     title: '清理缓存...',
									// });
									clearStorage();
									if (this.$user && this.$store && this.$store.state.user.accessToken) {
										this.$user.loginByToken(this.$store.state.user.accessToken);
									}
									this.$store.dispatch('mallConfig/actionResetConfig');
									// uni.hideLoading();
									uni.showToast({
										title: '清理完成',
										duration: 1000,
										icon: 'none',
									});
								}
							},
						});
						break;
					case 'map':
						uni.openLocation({
							latitude: Number(this.latitude),
							longitude: Number(this.longitude),
							name: this.address,
							address: this.address
						});
						break;
					case 'contact':
						if (wx.openCustomerServiceChat) {
							wx.openCustomerServiceChat({
								extInfo: {
									url: 'https://work.weixin.qq.com/kfid/kfc2132bcfa45f0fb28'
								},
								corpId: 'ww4e635c5ebdf96587',
								showMessageCard: true
							})
						} else {
							uni.showModal({
								title: '版本不支持',
								content: '请下载最新版本微信',
								showCancel: false,
								confirmText: '我知道了'
							})
						}
						break;
					case 'share':
						// uni.share({
						//     provider: 'weixin',
						//     type: 5,
						//     imageUrl: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png',
						//     title: '欢迎体验uniapp',
						//     miniProgram: {
						//         id: 'gh_abcdefg',
						//         path: 'pages/index/index',
						//         type: 0,
						//         webUrl: 'http://uniapp.dcloud.io'
						//     },
						//     success: ret => {
						//     }
						// });
						break;
					case '':
						this.$emit('btclick', this.url)
						break;
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	button {
		display: block;
		line-height: inherit;
		text-align: inherit;
		padding: 0 0;
		background: transparent;
		border: none;
		border-radius: 0;
		overflow: inherit;
		font-size: inherit;
		color: inherit;
	}

	button:after {
		display: none;
	}

	button.button-hover {
		color: inherit;
		background-color: transparent;
	}

	form {
		display: block;
		height: 100%;
		width: 100%;
	}

	.app-button-column {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.app-button-row {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.app-button-left {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}

	.app-button-top-cross-center {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-orient: vertical;
		-webkit-flex-direction: column;
		flex-direction: column;
		flex-wrap: nowrap;
		-webkit-align-items: center;
		align-items: center;
	}

	.app-left {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
	}

	.app-left-between {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-flex-direction: row;
		flex-direction: row;
		flex-wrap: nowrap;
		justify-content: space-between;
		align-items: center;
	}
</style>
